package com.android.settings.audio.peq.soundeffect;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.android.settings.audio.peq.util.ShareprefenceTool;
import com.android.settings.audio.peq.util.SmartPlayerApplication;


import java.util.List;

/**
 * Created by We on 2023/6/5
 */
public class SoundEffectManager {
    private final Gson gson;

    private SoundEffectManager() {
        this.gson = new Gson();
    }

    private static final class InstanceHolder {
        private static final SoundEffectManager instance = new SoundEffectManager();
    }

    public static SoundEffectManager getInstance() {
        return InstanceHolder.instance;
    }

    public SoundEffectConfig getSelectedPEQSoundEffectConfig() {
        return getSoundEffectConfigFromSharedPreference("sound_effect_peq");
    }

    public SoundEffectConfig getSelectedEQSoundEffectConfig() {
        return getSoundEffectConfigFromSharedPreference("sound_effect_eq");
    }

    public SoundEffectConfig getSelectedMSEBSoundEffectConfig() {
        return getSoundEffectConfigFromSharedPreference("sound_effect_mseb");
    }

    private SoundEffectConfig getSoundEffectConfigFromSharedPreference(String key) {
        String json = ShareprefenceTool.getInstance().getStringShareprefence(key, SmartPlayerApplication.getInstance(), "");
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        try {
            return gson.fromJson(json, SoundEffectConfig.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public void saveSoundEffectConfigAsPEQ(SoundEffectConfig soundEffectConfig) {
        ShareprefenceTool.getInstance().setStringSharedPreference("sound_effect_peq", toJsonString(soundEffectConfig), SmartPlayerApplication.getInstance());
    }

    public void saveSoundEffectConfigAsEQ(SoundEffectConfig soundEffectConfig) {
        ShareprefenceTool.getInstance().setStringSharedPreference("sound_effect_eq", toJsonString(soundEffectConfig), SmartPlayerApplication.getInstance());
    }

    public void saveSoundEffectConfigAsMSEB(SoundEffectConfig soundEffectConfig) {
        ShareprefenceTool.getInstance().setStringSharedPreference("sound_effect_mseb", toJsonString(soundEffectConfig), SmartPlayerApplication.getInstance());
    }

    private String toJsonString(SoundEffectConfig soundEffectConfig) {
        return gson.toJson(soundEffectConfig);
    }

    private String toJsonString(List<SoundEffectConfig> soundEffectConfigs) {
        return gson.toJson(soundEffectConfigs);
    }


    public void cacheSystemPrefabs(String type, List<SoundEffectConfig> soundEffectConfigList) {
        ShareprefenceTool.getInstance().setStringSharedPreference("systemPrefabs_" + type, toJsonString(soundEffectConfigList), SmartPlayerApplication.getInstance());
    }

    public List<SoundEffectConfig> getCachedSystemPrefabs(String type) {
        String s = ShareprefenceTool.getInstance().getStringShareprefence("systemPrefabs_" + type, SmartPlayerApplication.getInstance(), "");
        if (TextUtils.isEmpty(s)) {
            return null;
        }
        try {
            return gson.fromJson(s, new TypeToken<List<SoundEffectConfig>>() {
            }.getType());
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}
